package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.exception.BaseException;
import com.ruoyi.common.core.utils.ConvertUtil;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.params.SysDictDataParams;
import com.ruoyi.system.domain.vo.SysDictDataVo;
import com.ruoyi.system.domain.vo.SysDictLanguageVo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictLanguageMapper;
import com.ruoyi.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 字典 业务层处理
 *
 * @author ruoyi
 */
@Service
@Slf4j
public class SysDictDataServiceImpl implements ISysDictDataService {
    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private SysDictLanguageMapper sysDictLanguageMapper;

    @Value("${ruoyi.manage.devModel:false}")
    private Boolean authority;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public Page<SysDictData> selectDictDataList(Page<SysDictData> page, SysDictDataParams dictData) {
        return dictDataMapper.selectDictDataList(page,dictData);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType, String languageCode) {
        List<SysDictData> sysDictData = dictDataMapper.selectDictDataByType(dictType);
        if (sysDictData.size() == 0) {
            log.warn("未找到字典数据,dictType = {}", dictType);
            return sysDictData;
        }
        String language = LocaleContextHolder.getLocale().getLanguage();
        if (!"".equals(languageCode.trim()) && !"zh".equals(languageCode) || !Objects.equals(language, "zh")) {
            languageCode = language;
            List<Long> collect = sysDictData.stream().map(SysDictData::getDictCode).collect(Collectors.toList());
            List<SysDictLanguageVo> sysDictLanguageVos = sysDictLanguageMapper.getByDictCodesAndLanguageCode(collect, languageCode);
            Map<Long, String> valueMap = sysDictLanguageVos.stream().collect(Collectors.toMap(SysDictLanguageVo::getDictCode, SysDictLanguageVo::getValue));
            sysDictData.forEach(e -> e.setDictLabel(valueMap.get(e.getDictCode()) == null ? e.getDictLabel() : valueMap.get(e.getDictCode())));
        }
        return sysDictData;
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 通过字典ID删除字典数据信息
     *
     * @param dictCode 字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataById(Long dictCode) {
        return dictDataMapper.deleteDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataByIds(Long[] dictCodes) {
        return dictDataMapper.deleteDictDataByIds(dictCodes);
    }

    /**
     * 新增保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData dictData) {
        if (!authority) {
            throw new BaseException("该环境禁止新增字典");
        }
        return dictDataMapper.insertDictData(dictData);
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData dictData) {
        return dictDataMapper.updateDictData(dictData);
    }
}
